home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / Xpm / pixmap / Dialog.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  12KB  |  335 lines

  1. /* * Last edited: Dec 19 16:41 1991 (mallet) */
  2. /*
  3.  * $Id: Dialog.c,v 1.4 1992/10/16 16:40:15 mallet Exp $
  4.  * 
  5.  * Copyright 1991 Lionel Mallet
  6.  * 
  7.  * Permission to use, copy, modify, distribute, and sell this software and its
  8.  * documentation for any purpose is hereby granted without fee, provided that
  9.  * the above copyright notice appears in all copies and that both that
  10.  * copyright notice and this permission notice appear in supporting
  11.  * documentation, and that the name of Lionel MALLET not be used in
  12.  * advertising or publicity pertaining to distribution of the software
  13.  * without specific, written prior permission.  Lionel MALLET makes no
  14.  * representations about the suitability of this software for any
  15.  * purpose.  It is provided "as is" without express or implied warranty.
  16.  *
  17.  * Lionel MALLET DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
  18.  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  19.  * FITNESS, IN NO EVENT SHALL Lionel MALLET BE LIABLE FOR ANY SPECIAL,
  20.  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
  21.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION 
  22.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  23.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  24.  *
  25.  *  This software is opened and free. Furthermore, everybody is kindly
  26.  * invited to participate to improve it for the benefit of all.
  27.  * Improvements can be new features, bugs fixes and porting issues
  28.  * resolution.
  29.  *
  30.  * Author:  Lionel Mallet, SIMULOG
  31.  */
  32.  
  33. /*
  34.  * $XConsortium: Dialog.c,v 1.3 90/06/09 20:20:23 dmatic Exp $
  35.  *
  36.  * Copyright 1989 Massachusetts Institute of Technology
  37.  *
  38.  * Permission to use, copy, modify, distribute, and sell this software and its
  39.  * documentation for any purpose is hereby granted without fee, provided that
  40.  * the above copyright notice appear in all copies and that both that
  41.  * copyright notice and this permission notice appear in supporting
  42.  * documentation, and that the name of M.I.T. not be used in advertising or
  43.  * publicity pertaining to distribution of the software without specific,
  44.  * written prior permission.  M.I.T. makes no representations about the
  45.  * suitability of this software for any purpose.  It is provided "as is"
  46.  * without express or implied warranty.
  47.  *
  48.  * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  49.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
  50.  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  51.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  52.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  53.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  54.  *
  55.  * Author:  Davor Matic, MIT X Consortium
  56.  */
  57.  
  58.  
  59.  
  60. #include <X11/Intrinsic.h>
  61. #include <X11/StringDefs.h>
  62. #include <X11/Xos.h>
  63. #ifndef USE_ATHENA
  64. #include <Xm/SelectioB.h>
  65. #include <Xm/Text.h>
  66. #include <Xm/PushB.h>
  67. #else USE_ATHENA
  68. #include <X11/Shell.h>
  69. #include <X11/Xaw/Dialog.h>
  70. #include <X11/Xaw/Command.h>
  71. #endif
  72.     
  73. #include "Dialog.h"
  74.  
  75. #define min(x, y)                     (((x) < (y)) ? (x) : (y))
  76. #define max(x, y)                     (((x) > (y)) ? (x) : (y))
  77.  
  78. /* extern definitions from PixEdit.c */
  79. extern void unsetKillfromWM();
  80. extern XtAppContext pixmap_context;
  81.  
  82. static int selected;
  83.  
  84. static void SetSelected(w, client_data, call_data)
  85.      Widget w;
  86.      XtPointer client_data, call_data;
  87. {
  88.   int i;
  89.     
  90.   selected = (int)client_data;
  91. }
  92.  
  93. #ifdef USE_ATHENA
  94. static void SetOkay(w)
  95.      Widget w;
  96. {
  97.   SetSelected(w, (XtPointer)(Okay | Yes), NULL);
  98. }
  99.  
  100. static XtActionsRec actions_table[] = {
  101.   {"set-okay", SetOkay},
  102. };
  103. #endif
  104.  
  105. Dialog CreateDialog(top_widget, name, options)
  106.      Widget top_widget;
  107.      String name;
  108.      int options;
  109. {
  110.     int i;
  111.     Dialog popup;
  112.     Arg wargs[2];
  113.  
  114.     if (popup = (Dialog) XtMalloc(sizeof(_Dialog))) {
  115. #ifdef USE_ATHENA
  116.     XtAppAddActions(pixmap_context, actions_table,
  117.             XtNumber(actions_table));
  118. #endif
  119.     
  120.     popup->top_widget = top_widget;
  121. #ifndef USE_ATHENA
  122.     popup->dialog_widget = XmCreatePromptDialog(top_widget, name, NULL, 0);
  123.     popup->shell_widget = XtParent(popup->dialog_widget);
  124.  
  125.     XtUnmanageChild(XmSelectionBoxGetChild(popup->dialog_widget,
  126.                            XmDIALOG_HELP_BUTTON));
  127.     XtUnmanageChild(XmSelectionBoxGetChild(popup->dialog_widget,
  128.                            XmDIALOG_OK_BUTTON));
  129.     XtUnmanageChild(XmSelectionBoxGetChild(popup->dialog_widget,
  130.                            XmDIALOG_APPLY_BUTTON));
  131.     XtUnmanageChild(XmSelectionBoxGetChild(popup->dialog_widget,
  132.                            XmDIALOG_CANCEL_BUTTON));
  133.     
  134.     for (i = 0; i < XtNumber(dialog_buttons); i++)
  135.         if (options & dialog_buttons[i].flag)
  136.           switch (dialog_buttons[i].flag)
  137.         {
  138.             XmString xmstr;
  139.             
  140.         case Okay:
  141.         case Yes:
  142.         case Abort:
  143.           xmstr = XmStringCreateLtoR(dialog_buttons[i].name,
  144.                          XmSTRING_DEFAULT_CHARSET);
  145.           XtSetArg(wargs[0], XmNlabelString, xmstr);
  146.           XtSetValues(XmSelectionBoxGetChild(popup->dialog_widget,
  147.                                XmDIALOG_OK_BUTTON),
  148.                   wargs, 1);
  149.           XtManageChild(XmSelectionBoxGetChild(popup->dialog_widget,
  150.                                XmDIALOG_OK_BUTTON));
  151.                   XtRemoveAllCallbacks(XmSelectionBoxGetChild(
  152.                                                        popup->dialog_widget,
  153.                                                        XmDIALOG_OK_BUTTON),
  154.                                        XmNactivateCallback);
  155.           XtAddCallback(XmSelectionBoxGetChild(popup->dialog_widget,
  156.                                XmDIALOG_OK_BUTTON),
  157.                 XmNactivateCallback, SetSelected, 
  158.                 (XtPointer)dialog_buttons[i].flag);
  159.           XmStringFree(xmstr);
  160.           break;
  161.         case No:
  162.           xmstr = XmStringCreateLtoR(dialog_buttons[i].name,
  163.                          XmSTRING_DEFAULT_CHARSET);
  164.           XtSetArg(wargs[0], XmNlabelString, xmstr);
  165.           XtSetValues(XmSelectionBoxGetChild(popup->dialog_widget,
  166.                                XmDIALOG_APPLY_BUTTON),
  167.                   wargs, 1);
  168.           XtManageChild(XmSelectionBoxGetChild(popup->dialog_widget,
  169.                                XmDIALOG_APPLY_BUTTON));
  170.                   XtRemoveAllCallbacks(XmSelectionBoxGetChild(
  171.                                                        popup->dialog_widget,
  172.                                                        XmDIALOG_APPLY_BUTTON),
  173.                                        XmNactivateCallback);
  174.           XtAddCallback(XmSelectionBoxGetChild(popup->dialog_widget,
  175.                                XmDIALOG_APPLY_BUTTON),
  176.                 XmNactivateCallback, SetSelected, 
  177.                 (XtPointer)dialog_buttons[i].flag);
  178.           XmStringFree(xmstr);
  179.           break;
  180.         case Cancel:
  181.         case Retry:
  182.           xmstr = XmStringCreateLtoR(dialog_buttons[i].name,
  183.                          XmSTRING_DEFAULT_CHARSET);
  184.           XtSetArg(wargs[0], XmNlabelString, xmstr);
  185.           XtSetValues(XmSelectionBoxGetChild(popup->dialog_widget,
  186.                                XmDIALOG_CANCEL_BUTTON),
  187.                   wargs, 1);
  188.           XtManageChild(XmSelectionBoxGetChild(popup->dialog_widget,
  189.                               XmDIALOG_CANCEL_BUTTON));
  190.                   XtRemoveAllCallbacks(XmSelectionBoxGetChild(
  191.                                                        popup->dialog_widget,
  192.                                                        XmDIALOG_CANCEL_BUTTON),
  193.                                        XmNactivateCallback);
  194.           XtAddCallback(XmSelectionBoxGetChild(popup->dialog_widget,
  195.                                XmDIALOG_CANCEL_BUTTON),
  196.                 XmNactivateCallback, SetSelected, 
  197.                 (XtPointer)dialog_buttons[i].flag);
  198.           XmStringFree(xmstr);
  199.           break;
  200.         }
  201. #else USE_ATHENA
  202.     popup->shell_widget = XtCreatePopupShell(name, 
  203.                          transientShellWidgetClass, 
  204.                          top_widget, NULL, 0);
  205.     popup->dialog_widget = XtCreateManagedWidget("dialog", 
  206.                              dialogWidgetClass,
  207.                              popup->shell_widget, 
  208.                              NULL, 0);
  209.     for (i = 0; i < XtNumber(dialog_buttons); i++)
  210.         if (options & dialog_buttons[i].flag)
  211.         XawDialogAddButton(popup->dialog_widget, 
  212.                    dialog_buttons[i].name, SetSelected, 
  213.                                    (XtPointer)dialog_buttons[i].flag);
  214. #endif USE_ATHENA
  215.  
  216.     popup->options = options;
  217.     return popup;
  218.     }
  219.     else
  220.     return NULL;
  221. }
  222.  
  223. void PopdownDialog(popup, answer)
  224.     Dialog popup;
  225.     String *answer;
  226. {
  227.     if (answer)
  228. #ifndef USE_ATHENA
  229.     *answer = XmTextGetString(XmSelectionBoxGetChild(popup->dialog_widget,
  230.                              XmDIALOG_TEXT));
  231.     
  232.     XtUnmanageChild(popup->dialog_widget);
  233. #else USE_ATHENA
  234.     *answer = XawDialogGetValueString(popup->dialog_widget);
  235.     
  236.     XtPopdown(popup->shell_widget);
  237. #endif USE_ATHENA
  238. }
  239.  
  240. int PopupDialog(popup, message, suggestion, answer, grab)
  241.     Dialog popup;
  242.     String message, suggestion, *answer;
  243.     XtGrabKind grab;
  244. {
  245.   Position popup_x, popup_y, top_x, top_y;
  246.   Dimension popup_width, popup_height, top_width, top_height, border_width;
  247.   int n;
  248.   Arg wargs[4];
  249. #ifndef USE_ATHENA
  250.   XmString xmlabel, xmsuggestion;
  251.  
  252.   xmlabel = XmStringCreateLtoR(message, XmSTRING_DEFAULT_CHARSET);
  253.   n = 0;
  254.   XtSetArg(wargs[n], XmNselectionLabelString, xmlabel); n++;
  255.   if (suggestion)
  256.     {
  257.       xmsuggestion = XmStringCreateLtoR(suggestion, XmSTRING_DEFAULT_CHARSET);
  258.       XtSetArg(wargs[n], XmNtextString, xmsuggestion); n++;
  259.       XtManageChild(XmSelectionBoxGetChild(popup->dialog_widget, 
  260.                        XmDIALOG_TEXT));
  261.       XtRemoveAllCallbacks(XmSelectionBoxGetChild(popup->dialog_widget,
  262.                                                   XmDIALOG_TEXT),
  263.                            XmNactivateCallback);
  264.       XtAddCallback(XmSelectionBoxGetChild(popup->dialog_widget,XmDIALOG_TEXT),
  265.             XmNactivateCallback, SetSelected, (XtPointer)(Okay | Yes));
  266.     }
  267.   else XtUnmanageChild(XmSelectionBoxGetChild(popup->dialog_widget, 
  268.                           XmDIALOG_TEXT));
  269.   XtSetValues(popup->dialog_widget, wargs, n);
  270.   XmStringFree(xmlabel);
  271.   if (suggestion) XmStringFree(xmsuggestion);
  272. #else USE_ATHENA
  273.   n = 0;
  274.   XtSetArg(wargs[n], XtNlabel, message); n++;
  275.   XtSetArg(wargs[n], XtNvalue, suggestion); n++;
  276.   XtSetValues(popup->dialog_widget, wargs, n);
  277.  
  278.   XtRealizeWidget(popup->shell_widget);
  279. #endif USE_ATHENA
  280.   
  281.   n = 0;
  282.   XtSetArg(wargs[n], XtNx, &top_x); n++;
  283.   XtSetArg(wargs[n], XtNy, &top_y); n++;
  284.   XtSetArg(wargs[n], XtNwidth, &top_width); n++;
  285.   XtSetArg(wargs[n], XtNheight, &top_height); n++;
  286.   XtGetValues(popup->top_widget, wargs, n);
  287.  
  288.   n = 0;
  289.   XtSetArg(wargs[n], XtNwidth, &popup_width); n++;
  290.   XtSetArg(wargs[n], XtNheight, &popup_height); n++;
  291.   XtSetArg(wargs[n], XtNborderWidth, &border_width); n++;
  292.   XtGetValues(popup->shell_widget, wargs, n);
  293.  
  294.   popup_x = max(0, 
  295.         min(top_x + ((Position)top_width - (Position)popup_width) / 2, 
  296.             (Position)DisplayWidth(XtDisplay(popup->shell_widget), 
  297.                    DefaultScreen(XtDisplay(popup->shell_widget))) -
  298.             (Position)popup_width - 2 * (Position)border_width));
  299.   popup_y = max(0, 
  300.         min(top_y+((Position)top_height - (Position)popup_height) / 2,
  301.             (Position)DisplayHeight(XtDisplay(popup->shell_widget), 
  302.                    DefaultScreen(XtDisplay(popup->shell_widget))) -
  303.             (Position)popup_height - 2 * (Position)border_width));
  304.   n = 0;
  305.   XtSetArg(wargs[n], XtNx, popup_x); n++;
  306.   XtSetArg(wargs[n], XtNy, popup_y); n++;
  307.   XtSetValues(popup->shell_widget, wargs, n);
  308.  
  309.   selected = Empty;
  310.  
  311. #ifndef USE_ATHENA
  312.   XtManageChild(popup->dialog_widget);
  313. #else USE_ATHENA
  314.   XtPopup(popup->shell_widget, grab);
  315. #endif USE_ATHENA
  316.   unsetKillfromWM(popup->shell_widget);
  317.  
  318. #ifndef sparc
  319.   XtSetKeyboardFocus(popup->top_widget, popup->shell_widget);
  320. #endif sparc
  321.   
  322.   while ((selected & popup->options) == Empty) {
  323.       XEvent event;
  324.       XtAppNextEvent(pixmap_context, &event);
  325.       XtDispatchEvent(&event);
  326.   }
  327.  
  328.   PopdownDialog(popup, answer);
  329.  
  330.   return (selected & popup->options);
  331. }
  332.  
  333.  
  334.  
  335.